import requests
from threading import Thread
from multiprocessing import Process
import time


def request_bilibili():
    url = "https://www.bilibili.com/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
    }
    response = requests.get(url=url, headers=headers)
    print(response)

def math_test():
    number = 2
    while number<10000000:
        number**=2
    print("math_test done")



if __name__ == '__main__':
    start_time = time.time()
    thread_array = []
    for i in range(10):
        t = Thread(target=request_bilibili)
        thread_array.append(t)
        t.start()
    for t in thread_array:
        t.join()
    end_time = time.time()
    print(f"多线程获取10次bilibili首页运行了{end_time-start_time} s")

    start_time2 = time.time()
    process_array = []
    for j in range(10):
        p = Process(target=math_test)
        process_array.append(p)
        p.start()
    for p in process_array:
        p.join()
    end_time2 = time.time()
    print(f"多进程运行10次计算密集型程序math_test用了{end_time2-start_time2} s")


